package com.java2nb.novel.pay.dto;

import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * 查询支付订单请求DTO
 */
@Data
@Validated
public class QueryPayOrderRequestDTO {
    @NotBlank(message = "商户号mchNo不能为空")
    @Length(max = 30, message = "商户号mchNo长度不能超过30")
    private String mchNo;

    @NotBlank(message = "应用IDappId不能为空")
    @Length(max = 24, message = "应用IDappId长度不能超过24")
    private String appId;

    @Length(max = 30, message = "支付订单号payOrderId长度不能超过30")
    private String payOrderId; // 与mchOrderNo二选一

    @Length(max = 30, message = "商户订单号mchOrderNo长度不能超过30")
    private String mchOrderNo; // 与payOrderId二选一

    @NotNull(message = "请求时间reqTime不能为空")
    private Long reqTime; // 13位时间戳

    @NotBlank(message = "接口版本version不能为空")
    @Pattern(regexp = "1.0", message = "接口版本version固定为1.0")
    private String version = "1.0";

    @NotBlank(message = "签名sign不能为空")
    private String sign;

    @NotBlank(message = "签名类型signType不能为空")
    @Pattern(regexp = "MD5", message = "签名类型signType固定为MD5")
    private String signType = "MD5";

    // 校验payOrderId和mchOrderNo二选一
    @AssertTrue(message = "payOrderId和mchOrderNo必须传一个")
    public boolean isEitherPayOrderIdOrMchOrderNo() {
        return (payOrderId != null && !payOrderId.isEmpty())
                || (mchOrderNo != null && !mchOrderNo.isEmpty());
    }
}